00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _newton_raphson_solver_implementation_hpp_
00021 #define _newton_raphson_solver_implementation_hpp_
00022
00023 #include <iostream>
00024 #include <boost/scoped_ptr.hpp>
00025 #include "nonlinear_solver_functions.hpp"
00026 #include "nonlinear_solver_implementation.hpp"
00027 #include "linear_solver.hpp"
00028
00029 namespace gridpack {
00030 namespace math {
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 template <typename T, typename I>
00065 class NewtonRaphsonSolverImplementation
00066 : public NonlinearSolverImplementation<T, I>
00067 {
00068 public:
00069
00070 typedef typename NonlinearSolverImplementation<T, I>::VectorType VectorType;
00071 typedef typename NonlinearSolverImplementation<T, I>::JacobianBuilder JacobianBuilder;
00072 typedef typename NonlinearSolverImplementation<T, I>::FunctionBuilder FunctionBuilder;
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 NewtonRaphsonSolverImplementation(const parallel::Communicator& comm,
00089 const int& local_size,
00090 JacobianBuilder form_jacobian,
00091 FunctionBuilder form_function)
00092 : NonlinearSolverImplementation<T, I>(comm, local_size, form_jacobian, form_function),
00093 p_linear_solver()
00094 {
00095 this->configurationKey("NewtonRaphsonSolver");
00096 }
00097
00098
00099
00100 NewtonRaphsonSolverImplementation(MatrixT<T, I>& J,
00101 JacobianBuilder form_jacobian,
00102 FunctionBuilder form_function)
00103 : NonlinearSolverImplementation<T, I>(J, form_jacobian, form_function),
00104 p_linear_solver()
00105 {
00106 this->configurationKey("NewtonRaphsonSolver");
00107 }
00108
00109
00110
00111
00112
00113
00114
00115 ~NewtonRaphsonSolverImplementation(void)
00116 {
00117 }
00118
00119
00120 protected:
00121
00122
00123
00124
00125
00126
00127 double p_tolerance;
00128
00129
00130 int p_max_iterations;
00131
00132
00133 boost::scoped_ptr< LinearSolverT<T, I> > p_linear_solver;
00134
00135
00136 void p_solve(VectorType& x)
00137 {
00138 NonlinearSolverImplementation<T, I>::p_solve(x);
00139 double stol(1.0e+30);
00140 double ftol(1.0e+30);
00141 int iter(0);
00142
00143 boost::scoped_ptr<VectorType> deltaX(this->p_X->clone());
00144 while (stol > this->p_solutionTolerance && iter < this->p_maxIterations) {
00145 this->p_function(*(this->p_X), *(this->p_F));
00146 this->p_F->scale(-1.0);
00147 this->p_jacobian(*(this->p_X), *(this->p_J));
00148 if (!p_linear_solver) {
00149 p_linear_solver.reset(new LinearSolverT<T, I>(*(this->p_J)));
00150 p_linear_solver->configure(this->p_configCursor);
00151 }
00152 deltaX->zero();
00153 p_linear_solver->solve(*(this->p_F), *deltaX);
00154 stol = deltaX->norm2();
00155 ftol = this->p_F->norm2();
00156 this->p_X->add(*deltaX);
00157 iter += 1;
00158 if (this->processor_rank() == 0) {
00159 std::cout << "Newton-Raphson "
00160 << "iteration " << iter << ": "
00161 << "solution residual norm = " << stol << ", "
00162 << "function norm = " << ftol
00163 << std::endl;
00164 }
00165 }
00166 }
00167
00168 };
00169
00170
00171
00172 }
00173 }
00174 #endif